<?php
class FormProcessor_UploadImage extends FormProcessor {
	public $image;
	protected $db;
	public function __construct($db, $albumid) {
		$this->db = $db;
		parent::__construct($this->db);
		
		$this->image = new DatabaseObject_Image($this->db);
		$this->image->albumid = $albumid;
	}
	
	public function process(Zend_Controller_Request_Abstract $request) {
		
		$this->introduce = $this->sanitize($request->getPost('introduce'));
		
		if (strlen($this->introduce) != 0) {
			$this->image->introduce = $this->introduce;
		}
		if (!isset($_FILES['image']) || !is_array($_FILES['image'])) {
			$this->addError('image', '上传非法数据');
			return false;
		}
		
		$file = $_FILES['image'];
		
		switch ($file['error']) {
			case UPLOAD_ERR_OK:
			     //成功
			     break;
			case UPLOAD_ERR_FORM_SIZE :
			     //使用设置大小
			     break;
			case UPLOAD_ERR_INI_SIZE :
				$this->addError('image', '上传文件过大');
				break;
			case UPLOAD_ERR_PARTIAL :
				$this->addError('image', '上传文件不完整');
				break;
			case UPLOAD_ERR_NO_FILE :
				$this->addError('image', '没有文件上传');
				break;
			case UPLOAD_ERR_NO_TMP_DIR :
				$this->addError('image', '没有找到临时文件');
				break;
			case UPLOAD_ERR_CANT_WRITE :
				$this->addError('image', '不能写入文件');
				break;
			case UPLOAD_ERR_EXTENSION :
				$this->addError('image', '非法文件后缀');
				break;
			default:
				$this->addError('image', '发生未知错误');
			if ($this->hasError()) {
				return;
			}
		}
		
	    $info = getimagesize($file['tmp_name']);
        if (!$info) {
        	$this->addError('type', '上传文件不是图片');
            return false;
        }
        switch ($info[2]) {
        	case IMAGETYPE_PNG  :
        	case IMAGETYPE_GIF  :
        	case IMAGETYPE_JPEG :
        		break;
            default:
                $this->addError('type', '文件格式不正确');
                return false;
        }
        
        if (!$this->hasError()) {
        	$this->image->uploadFile($file['tmp_name']);
        	$this->image->filename = basename($file['name']);
        	$this->image->dateline = time();
        	$this->image->save();
        }
		return !$this->hasError();
	}
}
?>